package com.book.common.core.domain.model;

import com.alibaba.fastjson2.annotation.JSONField;
import com.book.common.core.domain.entity.AppUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;

/**
 * @ClassName AppLoginUser * @Description App用户登录的信息
 * @Author Bigboss
 * @Date 14:48 2024/10/19
 * @Version 1.0
 **/
public class AppLoginUser implements UserDetails {

    private static final long serialVersionUID = 1L;

    private String userId;


    /**
     * 用户唯一标识
     */
    private String token;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 权限列表
     */
    private Set<String> permissions;

    /**
     * 用户信息
     */
    private AppUser user;


    public AppLoginUser()
    {
    }

    public AppLoginUser(AppUser user, Set<String> permissions)
    {
        this.user = user;
        this.permissions = permissions;
    }

    public AppLoginUser(String userId, AppUser user, Set<String> permissions)
    {
        this.userId = userId;
        this.user = user;
        this.permissions = permissions;
    }


    public String getUserId() {
        return userId;
    }

    public AppLoginUser setUserId(String userId) {
        this.userId = userId;
        return this;
    }

    public String getToken() {
        return token;
    }

    public AppLoginUser setToken(String token) {
        this.token = token;
        return this;
    }

    public Long getLoginTime() {
        return loginTime;
    }

    public AppLoginUser setLoginTime(Long loginTime) {
        this.loginTime = loginTime;
        return this;
    }

    public Long getExpireTime() {
        return expireTime;
    }

    public AppLoginUser setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
        return this;
    }

    public String getIpaddr() {
        return ipaddr;
    }

    public AppLoginUser setIpaddr(String ipaddr) {
        this.ipaddr = ipaddr;
        return this;
    }

    public String getLoginLocation() {
        return loginLocation;
    }

    public AppLoginUser setLoginLocation(String loginLocation) {
        this.loginLocation = loginLocation;
        return this;
    }

    public String getBrowser() {
        return browser;
    }

    public AppLoginUser setBrowser(String browser) {
        this.browser = browser;
        return this;
    }

    public String getOs() {
        return os;
    }

    public AppLoginUser setOs(String os) {
        this.os = os;
        return this;
    }

    public Set<String> getPermissions() {
        return permissions;
    }

    public AppLoginUser setPermissions(Set<String> permissions) {
        this.permissions = permissions;
        return this;
    }

    public AppUser getUser() {
        return user;
    }

    public AppLoginUser setUser(AppUser user) {
        this.user = user;
        return this;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @JSONField(serialize = false)
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getPhone();
    }
    /**
     * 账户是否未过期,过期无法验证
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonExpired()
    {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     *
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isAccountNonLocked()
    {
        return true;
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     *
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isCredentialsNonExpired()
    {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     *
     * @return
     */
    @JSONField(serialize = false)
    @Override
    public boolean isEnabled()
    {
        return true;
    }
}
